package cn.toma.common.enumtype.status.member;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @Author: zqx
 * @Date: 2019-08-02 14:13
 * @Description: 状态枚举
 */
@MappedJdbcTypes(JdbcType.INTEGER)
@MappedTypes(StatusEnum.class)
public class TypeEnumTypeHandler extends BaseTypeHandler<TypeEnum>
{
	@Override
	public void setNonNullParameter(PreparedStatement preparedStatement, int i, TypeEnum statusEnumType, JdbcType jdbcType) throws SQLException
	{
		preparedStatement.setInt(i, statusEnumType.getCode());
	}

	@Override
	public TypeEnum getNullableResult(ResultSet resultSet, String s) throws SQLException
	{

		int anInt = resultSet.getInt(s);
		TypeEnum statusByCode = TypeEnum.getStatusByCode(anInt);
		return statusByCode;
	}

	@Override
	public TypeEnum getNullableResult(ResultSet resultSet, int i) throws SQLException
	{
		return TypeEnum.getStatusByCode(resultSet.getInt(i));
	}

	@Override
	public TypeEnum getNullableResult(CallableStatement callableStatement, int i) throws SQLException
	{
		return TypeEnum.getStatusByCode(callableStatement.getInt(i));
	}
}
